package com.seari.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.seari.bean.MenuPrivilege;
import com.seari.mapper.PrivilegeMapper;
import com.seari.mapper.RoleMapper;
import com.seari.mapper.RolePrivilegeMapper;
import com.seari.pojo.QueryMenuPrivilege;
import com.seari.service.PrivilegeService;

@Service("PrivilegeService")
public class PrivilegeServiceImpl implements PrivilegeService {
	
	@Autowired
	private PrivilegeMapper privilegeMapper;
	@Autowired
	private RolePrivilegeMapper rolePrivilegeMapper;
	@Autowired
	private RoleMapper roleMapper;
	

	@Override
	public List<MenuPrivilege> privilegeSearch(QueryMenuPrivilege qu) {
		// TODO Auto-generated method stub
		 List<MenuPrivilege> privileges = privilegeMapper.privilegeSearch(qu);
		 for (int i = 0; i < privileges.size(); i++) {
			 int parentId = privileges.get(i).getParentId();
			if (parentId != 0) {
				String parentName = roleMapper.getPmNameByParentId(parentId);
				privileges.get(i).setParentName(parentName);
			}
		}
		 return privileges;
	}

	@Override
	public List<MenuPrivilege> getPrivilegeByMpid(int mpid) {
		// TODO Auto-generated method stub
		return privilegeMapper.getPrivilegeByMpid(mpid);
	}

	@Override
	public List<MenuPrivilege> getPrivileges(QueryMenuPrivilege qmp) {
		// TODO Auto-generated method stub
		 List<MenuPrivilege> privileges = privilegeMapper.getPrivileges(qmp);
		 for (int i = 0; i < privileges.size(); i++) {
			 int parentId = privileges.get(i).getParentId();
			if (parentId != 0) {
				String parentName = roleMapper.getPmNameByParentId(parentId);
				privileges.get(i).setParentName(parentName);
			}
		}
		 
		 return privileges;
	}

	@Override
	public int checkMenuPrivilegeExist(QueryMenuPrivilege qup) {
		// TODO Auto-generated method stub
		return privilegeMapper.checkMenuPrivilegeExist(qup);
	}

	@Override
	public int updatePrivilege(MenuPrivilege menuPrivilege) {
		// TODO Auto-generated method stub
		return privilegeMapper.updatePrivilege(menuPrivilege);
	}

	@Override
	public int addPrivilege(MenuPrivilege menuPrivilege) {
		// TODO Auto-generated method stub
		return privilegeMapper.addPrivilege(menuPrivilege);
	}

	@Override
	public int deletePrivilege(int mpid) {
		// TODO Auto-generated method stub
		// 删除中间表拥有该按钮权限的角色
		rolePrivilegeMapper.deleteRolePrivilegeByUupid(mpid);
		return privilegeMapper.deletePrivilege(mpid);
	}

	@Override
	public Integer getPrivilegesCount(QueryMenuPrivilege qmp) {
		// TODO Auto-generated method stub
		return privilegeMapper.getPrivilegesCount(qmp);
	}

	@Override
	public Integer privilegeSearchCount(QueryMenuPrivilege qmp) {
		// TODO Auto-generated method stub
		return privilegeMapper.privilegeSearchCount(qmp);
	}

}
